<?php

namespace app\en_admnim\controller;

use app\en_admnim\model\BannerModel;
use \think\Config;
class Banner extends Base
{
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $banner = new BannerModel();
            $selectResult = $banner->getBannerByWhere($where, $offset, $limit);
            $shop_type = Config('SHOW_TYPE');
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['thumbnail'] = '<img src="' . $vo['thumbnail'] . '" width="40px" height="40px">';
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
                $selectResult[$key]['type'] = $shop_type[$vo['isuse']];
            }
            $return['total'] = $banner->getAllBanner($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    // 添加banner
    public function bannerAdd()
    {
        if(request()->isPost()){
            $param = input('post.');

            unset($param['file']);
            $param['add_time'] = date('Y-m-d H:i:s');

            $banner = new BannerModel();
            $flag = $banner->addBanner($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        return $this->fetch();
    }

    public function bannerEdit()
    {
        $banner = new BannerModel();
        if(request()->isPost()){

            $param = input('post.');
            unset($param['file']);
            $flag = $banner->editBanner($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');
        $this->assign([
            'article' => $banner->getOneBanner($id)
        ]);
        return $this->fetch();
    }
    public function bannerDelete(){
        $id = input('param.id');

        $banner = new BannerModel();
        $flag = $banner->delBanner($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    public function bannerDel()
    {
        $id = input('param.id');
        $banner = new BannerModel();
        $use = $_GET['is_use'] ?? 0;
        $flag = $banner->editBannerUse($use,$id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    public function bannerOn()
    {
        $id = input('param.id');
        $banner = new BannerModel();
        $use = $_GET['is_use'] ?? 0;
        $flag = $banner->editBannerUse($use,$id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    public function bannerOff()
    {
        $id = input('param.id');
        $banner = new BannerModel();
        $use = input('param.is_use');
        $flag = $banner->editBannerUse($use,$id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    
// 上传图片
    public function uploadImg()
    {
        if (request()->isAjax()) {

            $file = request()->file('file');
// 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if ($info) {
                $src = '/upload/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            } else {
// 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'banner/banneredit',
                'href' => url('banner/banneredit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '禁用' => [
                'auth' => 'banner/banneroff',
                'href' => "javascript:bannerOff(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
            '启用' => [
                'auth' => 'banner/banneron',
                'href' => "javascript:bannerOn(" . $id . ")",
                'btnStyle' => 'primary',
                'icon' => 'fa fa-trash-o'
            ],
            '删除' => [
                'auth' => 'banner/delete',
                'href' => "javascript:delete(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}